package com.tefl.common.util;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tefl.common.value.TreeNode;


/**
 * 转换工具类
 * 
 * @author liuxin
 * 
 */
public class ConvertHelper {

	/**
	 * 将一个List集合按照指定属性转换成Map键值对，以指定属性为键
	 * 
	 * @param srcList
	 *            源List集合
	 * @param key
	 *            指定属性，键
	 * @return 转换所得的Map
	 * @throws IntrospectionException
	 *             内省期间发生异常
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public static Map transferListToMap(List srcList, String key)
			throws IntrospectionException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		
		Map destMap = null;

		if (ValidateHelper.isNotEmptyCollection(srcList)) {
			destMap = new HashMap();
			for (Object object : srcList) {
				PropertyDescriptor pd = new PropertyDescriptor(key,
						object.getClass());
				Object keyValue = pd.getReadMethod().invoke(object);
				destMap.put(keyValue, object);
			}
		}

		return destMap;
	}
	
	/**
	 * 将一个源List集合按照父节点标识，转换成一棵树
	 * @param srcList 源List集合
	 * @param superDef 父节点标识
	 * @return 树
	 */
	public static List<TreeNode> transferListToTree(List srcList, String superDef){
		return null;
	}
}
